/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import java.util.HashMap;
import java.util.Map;
import com.vng.jcore.common.Config;
import db.Redis_Rd;
import libCore.Util;
import org.json.simple.JSONValue;

/**
 *
 * @author Linhta
 */
public class ConfigDB {

    private ConfigDB() {
    }

    public static ConfigDB getInstance() {
        return NewSingletonHolder.INSTANCE;
    }
//    

    private static class NewSingletonHolder {

        private static final ConfigDB INSTANCE = new ConfigDB();
    }

    public static String verConfig() {
        String verConfig = "";

        verConfig = Config.getParam("config", "version");

        return verConfig;
    }

    public static Map<String, Object> getConfig() {
        Map<String, Object> config = new HashMap<String, Object>();

        String[] keys;
        keys = Redis_Rd.getInstance().smember("config").toArray(new String[0]);
        for (String key : keys) {
            Map<String, Object> val = (HashMap<String, Object>) converStringToObj(Redis_Rd.getInstance().hget(key));
            config.put(key, val);
        }

        return config;
    }

    public static Map<String, Object> getObj(String key, Object obj) {
        Map<String, Object> data = new HashMap<String, Object>();

        Map<String, Object> o = libCore.Util.obj2Map(obj);

        if (o == null || o.isEmpty()) {
            data.put(key, obj);
        } else {
            for (Map.Entry<String, Object> entry : o.entrySet()) {
                String string = entry.getKey();
                Object object = entry.getValue();

                Map<String, Object> objReturn = getObj(string, object);
                if (objReturn.size() == 1) {
                    data.putAll(objReturn);
                } else {
                    data.put(string, objReturn);// = getObj(string, object, val);
                }
            }
        }
        return data;
    }

    public static Map<String, Object> converStringToObj(Map<String, String> str) {
        Map<String, Object> obj = new HashMap<String, Object>();
        for (Map.Entry<String, String> entry : str.entrySet()) {
            String key = entry.getKey();
            String val = entry.getValue();

            obj.put(key, JSONValue.parse(val));
        }
        return obj;
    }

    public static void main(String[] args) {
        Map<String, Object> data = new HashMap<String, Object>();

        Map<String, Object> obj = new HashMap<String, Object>();
        obj = getConfig();
        obj = Util.obj2Map(obj.get(ConfigMacros.SHOP));
        for (Map.Entry<String, Object> entry : obj.entrySet()) {
            String string = entry.getKey();
            Object object = entry.getValue();

            data.put(string, getObj(string, object));
        }


    }

    public static Map<String, Object> getClientConfig() {
        Map<String, Object> data = new HashMap<String, Object>();

        Map<String, Object> obj = getConfig();
        for (Map.Entry<String, Object> entry : obj.entrySet()) {
            String string = entry.getKey();
            Object object = entry.getValue();

            data.put(string, getObj(string, object));
        }

        return data;
    }
//     public static Map<String,Object> getConfig2 ()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        HashMap<String,Conf_Cote> conf_cotes = ConfigModels.getInstance().conf_cotes;
//        HashMap<String,Conf_Peg> conf_pegs= ConfigModels.getInstance().conf_pegs;
//        Conf_Friend conf_friends = ConfigModels.getInstance().conf_friends;
//        HashMap<String,Conf_BoxEgg> conf_boxEggs = ConfigModels.getInstance().conf_boxEggs ;
//        HashMap<String,Conf_Egg> conf_eggs =ConfigModels.getInstance().conf_eggs;
//        HashMap<String,Conf_Ibshop_item> conf_ibshopItems = ConfigModels.getInstance().conf_ibshopItems;
//        HashMap<String,Conf_Mix> conf_mixs = ConfigModels.getInstance().conf_mixs;
//        HashMap<String,Conf_Mix_item> conf_mixItems = ConfigModels.getInstance().conf_mixItems;
//        HashMap<String,Conf_User> conf_user = ConfigModels.getInstance().conf_user;
//        
//        
//        return data;
//        
//    }
//     
//    public static Map<String,Object> getConf_Cotes()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_IbShops()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_MixItems()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_Mixs()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_BoxEggs()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_Eggs()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_Pegs()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_Friends()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//     
//     public static Map<String,Object> getConf_Users()
//    {
//        Map<String,Object> data = new HashMap<String, Object>();
//        
//        
//        return data;
//    }
//    
//     private Map<String,IBshop.Cote> getCotes()
//    {
//        Map<String,IBshop.Cote> cotes = new HashMap<String, IBshop.Cote>();
//        
//        return cotes;
//    }
//     
//      
//    private Map<String,IBshop.BoxEggs> getBoxs()
//    {
//        Map<String,IBshop.BoxEggs> boxs = new HashMap<String, IBshop.BoxEggs>();
//        
//        return boxs;
//    }
//      
//       
//    private Map<String,IBshop.Penguin> getPegs()
//    {
//        Map<String,IBshop.Penguin> peg = new HashMap<String, IBshop.Penguin>();
//        
//        return peg;
//    }
//       
//        
//    private Map<String,IBshop.Egg> getEggs()
//    {
//        Map<String,IBshop.Egg> getEggs = new HashMap<String, IBshop.Egg>();
//        
//        return getEggs;
//    }
//        
//         
//    private List<ConfigModels.Mix> getMix()
//    {
//        List<ConfigModels.Mix> getEggs = new ArrayList<ConfigModels.Mix>();
//        
//        return getEggs;
//    }
//         
//          
//    private List<ConfigModels.Action> getActions()
//    {
//       List<ConfigModels.Action> getActions = new ArrayList< ConfigModels.Action>();
//        
//        return getActions;
//    }
}
